package gui.Login;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import control.ComplaintController;
import control.LoginController;
import control.MessageController;
import control.OrderController;
import control.SetRatesController;

import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import vcpClasses.Notification;
import vcpClasses.RateSettingRequest;
import vcpClasses.Rates;
import vcpClasses.Reminder;

import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.toedter.calendar.JDateChooser;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JScrollBar;
/**
 * this class use  for Message Center
 * @author G8
 *
 */
public class MessageCenter extends JPanel implements ActionListener{
	public JButton btnLogout;
	public JButton btnBack;
	private JButton btnView;
	private JComboBox comboBoxMessages;
	private JTextArea textAreaMessageContent;
	private SetRatesController setRatesController;
	private String notifType;
	private String notifSender;
	private JButton btnYes;
	private JButton btnNo;
	private JTextField textFieldRefund;
	private JLabel lblReply;
	private JTextArea textAreaReply;
	private JLabel lblRefund;
	private JButton btnReply;
	private MessageController messageController;
	private ComplaintController complaintController;
	private JScrollPane scrollReply;
	private JScrollPane scrollContent;
	private OrderController orderController;
	/**
	 *Message Center  Constructor 
	 */
	public MessageCenter(){
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize() {
		messageController = new MessageController();
		complaintController = new ComplaintController();
		
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Message Center", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(MessageCenter.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		textAreaMessageContent = new JTextArea();
		textAreaMessageContent.setEditable(false);
		scrollContent = new JScrollPane (textAreaMessageContent, 
		JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollContent.setBounds(69, 156, 365, 164);
		add(scrollContent);
		
		
		btnLogout = new JButton("Logout");
		btnLogout.setSize(150, 30);
		btnLogout.setBounds(393, 22, 78, 35);
		btnLogout.addActionListener(this);
		add(btnLogout);
		
		JLabel lblMessage = new JLabel("Message:");
		lblMessage.setBounds(69, 71, 102, 14);
		add(lblMessage);
		
		final DefaultComboBoxModel model = new DefaultComboBoxModel(VCP.currentUser.getUnreadNotif());
		comboBoxMessages = new JComboBox(model);
		comboBoxMessages.setBounds(181, 68, 138, 20);
		add(comboBoxMessages);
		
		JLabel lblMessageContent = new JLabel("Content:");
		lblMessageContent.setBounds(68, 131, 92, 14);
		add(lblMessageContent);
		
		btnView = new JButton("View");
		btnView.setBounds(181, 99, 78, 20);
		btnView.addActionListener(this);
		add(btnView);
		
		btnYes = new JButton("Approve");
		btnYes.setBounds(140, 331, 92, 20);
		btnYes.setVisible(false);
		btnYes.addActionListener(this);
		add(btnYes);
		
		btnNo = new JButton("Deny");
		btnNo.setBounds(265, 331, 92, 20);
		btnNo.setVisible(false);
		btnNo.addActionListener(this);
		add(btnNo);
		
		lblReply = new JLabel("Reply:");
		lblReply.setBounds(69, 219, 91, 14);
		lblReply.setVisible(false);
		add(lblReply);
		
		textAreaReply = new JTextArea();
		scrollReply = new JScrollPane (textAreaReply, 
		JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollReply.setBounds(69, 244, 364, 52);
		add(scrollReply);
		
		lblRefund = new JLabel("Refund:");
		lblRefund.setBounds(69, 303, 91, 14);
		add(lblRefund);
		
		textFieldRefund = new JTextField();
		textFieldRefund.setBounds(140, 300, 86, 20);
		textFieldRefund.setVisible(false);
		add(textFieldRefund);
		textFieldRefund.setColumns(10);
		
		btnReply = new JButton("Reply");
		btnReply.setBounds(200, 331, 89, 23);
		btnReply.setVisible(false);
		btnReply.addActionListener(this);
		add(btnReply);
	}
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e) {	
		if (e.getSource() == btnView){
			notifType = new String(VCP.currentUser.getUnreadNotif().elementAt(comboBoxMessages.getSelectedIndex()).getType());
			notifSender = new String(VCP.currentUser.getUnreadNotif().elementAt(comboBoxMessages.getSelectedIndex()).getSender());
			textAreaMessageContent.setText(VCP.currentUser.getUnreadNotif().elementAt(comboBoxMessages.getSelectedIndex()).getContent().toString());
			if (notifType.equals("RATES") || notifType.equals("REMINDER")){
				btnYes.setVisible(true);
				btnNo.setVisible(true);
				}
			
			else if (notifType.equals("COMPLAINT")){
				scrollContent.setBounds(69, 156, 365, 60);
				lblReply.setVisible(true);
				scrollReply.setVisible(true);
				scrollReply.setEnabled(true);
				lblRefund.setVisible(true);
				textFieldRefund.setVisible(true);
				btnReply.setVisible(true);
				}
			
			else if (notifType.equals("COMPLAINT_RESPONSE") || notifType.equals("SUB_REMINDER") || notifType.equals("APPROVED_RATES") || notifType.equals("DENIED_RATES")){
				btnReply.setText("OK");
				btnReply.setVisible(true);
				}
			}
		if (e.getSource() == btnReply){
			Notification markThisRead = VCP.currentUser.getUnreadNotif().elementAt(comboBoxMessages.getSelectedIndex());
			if (notifType.equals("COMPLAINT")){
				Object[] replyContainer = new Object[2];
				replyContainer[0] = new Notification();
				((Notification)replyContainer[0]).setContent(textAreaReply.getText()+"\n\nYou will receive a refund of : "+textFieldRefund.getText());
				((Notification)replyContainer[0]).setReceiver(markThisRead.getSender());
				replyContainer[1] = Float.parseFloat(textFieldRefund.getText());
				SystemTypes replyToSend = new SystemTypes((Serializable)replyContainer,null);
				SystemTypes replyResult = complaintController.SendComplaintReply(replyToSend);
				if (replyResult.getSysMode().equals(SystemMode.OK))
					JOptionPane.showMessageDialog(null, replyResult.getObj().toString(), "Success",
						     JOptionPane.INFORMATION_MESSAGE);
						else
							JOptionPane.showMessageDialog(null, replyResult.getObj().toString(), "Error",
						       JOptionPane.ERROR_MESSAGE);
			}
			SystemTypes toMarkRead = new SystemTypes(markThisRead,null);
			SystemTypes markResult = messageController.SetNotificationRead(toMarkRead);
			if (markResult.getSysMode().equals(SystemMode.OK))
				JOptionPane.showMessageDialog(null, markResult.getObj().toString(), "Success",
				     JOptionPane.INFORMATION_MESSAGE);
				else
					JOptionPane.showMessageDialog(null, markResult.getObj().toString(), "Error",
				       JOptionPane.ERROR_MESSAGE);
				}
		if (e.getSource() == btnYes){
			if (notifType.equals("RATES")){
				RateSettingRequest ratesApproved = (RateSettingRequest)VCP.currentUser.getUnreadNotif().get(comboBoxMessages.getSelectedIndex());
				ratesApproved.setType("APPROVED_RATES");
				ratesApproved.setContent((ratesApproved.getContent().toString() + "\n\nNetwork Manager descision : APPROVED").toString());
				ApproveOrDenyRates(ratesApproved);
			}
			else if (notifType.equals("REMINDER")){
				Reminder lateYes = (Reminder)VCP.currentUser.getUnreadNotif().get(comboBoxMessages.getSelectedIndex());
				lateYes.setType("LATE");
				ReminderLateOrCancel(lateYes);
			}
			}
		if (e.getSource() == btnNo){
			if (notifType.equals("RATES")){
				RateSettingRequest ratesDenied = (RateSettingRequest)VCP.currentUser.getUnreadNotif().get(comboBoxMessages.getSelectedIndex());
				ratesDenied.setType("DENIED_RATES");
				ratesDenied.setContent((ratesDenied.getContent().toString() + "\n\nNetwork Manager descision : DENIED").toString());
				ApproveOrDenyRates(ratesDenied);
			}
			else if (notifType.equals("REMINDER")){
				Reminder lateNo = (Reminder)VCP.currentUser.getUnreadNotif().get(comboBoxMessages.getSelectedIndex());
				lateNo.setType("CANCELLED");
				ReminderLateOrCancel(lateNo);
			}
		}
		if (e.getSource() == btnBack){
			VCP.setScreen(this, VCP.prevScreen);
			VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
			VCP.prevScreen.setLayout(null);
			VCP.frame.setBounds(VCP.prevScreen.getBounds());
			}
		if (e.getSource() == btnLogout){
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	private void ApproveOrDenyRates(RateSettingRequest request){
		setRatesController = new SetRatesController();
		SystemTypes ratesToHandle = new SystemTypes(request,null);
		SystemTypes changeResult = setRatesController.ChangeRates(ratesToHandle);
		
		if (changeResult.getSysMode().equals(SystemMode.OK)){
			JOptionPane.showMessageDialog(null, changeResult.getObj().toString(), "Success",
			     JOptionPane.INFORMATION_MESSAGE);
			SystemTypes markResult = messageController.SetNotificationRead(ratesToHandle);
			if (markResult.getSysMode().equals(SystemMode.OK))
				JOptionPane.showMessageDialog(null, markResult.getObj().toString(), "Success",
				     JOptionPane.INFORMATION_MESSAGE);
				else
					JOptionPane.showMessageDialog(null, markResult.getObj().toString(), "Error",
				       JOptionPane.ERROR_MESSAGE);
				}
			else
				JOptionPane.showMessageDialog(null, changeResult.getObj().toString(), "Error",
			       JOptionPane.ERROR_MESSAGE);
			}
	
	private void ReminderLateOrCancel(Reminder reminder){
		orderController = new OrderController(null);
		SystemTypes reminderAnswer = new SystemTypes(reminder,null);
		SystemTypes answerResult = orderController.HandleReminderReply(reminderAnswer);
		if (answerResult.getSysMode().equals(SystemMode.OK)){
			JOptionPane.showMessageDialog(null, answerResult.getObj().toString(), "Success",
			     JOptionPane.INFORMATION_MESSAGE);
			SystemTypes markResult = messageController.SetNotificationRead(reminderAnswer);
			if (markResult.getSysMode().equals(SystemMode.OK))
				JOptionPane.showMessageDialog(null, markResult.getObj().toString(), "Success",
				     JOptionPane.INFORMATION_MESSAGE);
				else
					JOptionPane.showMessageDialog(null, markResult.getObj().toString(), "Error",
				       JOptionPane.ERROR_MESSAGE);
				}
			else
				JOptionPane.showMessageDialog(null, answerResult.getObj().toString(), "Error",
			       JOptionPane.ERROR_MESSAGE);
	}
}
